package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.dto.ModelingManagementFormDTO;
import com.ruoyi.business.domain.dto.ModelingManagementQueryDTO;
import com.ruoyi.business.domain.vo.ModelingManagementVO;
import com.ruoyi.business.domain.vo.PowerStationVO;
import com.ruoyi.business.mapper.ModelingManagementMapper;
import com.ruoyi.business.mapper.NumberManagementMapper;
import com.ruoyi.business.service.IModelingManagementService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 建模管理Service业务层处理
 *
 * @author yangmeng
 * @date 2023-06-01
 */
@Service
public class ModelingManagementServiceImpl implements IModelingManagementService {

    @Autowired
    private ModelingManagementMapper modelingManagementMapper;

    @Autowired
    private NumberManagementMapper numberManagementMapper;

    /**
     * 查询建模管理
     *
     * @param modelingId 建模管理主键
     * @return 建模管理
     */
    @Override
    public ModelingManagementVO selectModelingManagementByModelingId(Long modelingId) {
        return modelingManagementMapper.selectModelingManagementByModelingId(modelingId);
    }

    /**
     * 查询建模管理列表
     *
     * @param modelingManagementQueryDTO 建模管理
     * @return 建模管理
     */
    @Override
    public List<ModelingManagementVO> selectModelingManagementList(ModelingManagementQueryDTO modelingManagementQueryDTO) {
        return modelingManagementMapper.selectModelingManagementList(modelingManagementQueryDTO);
    }

    /**
     * 新增建模管理
     *
     * @param modelingManagementFormDTO 建模管理
     * @return 结果
     */
    @Override
    public ModelingManagementFormDTO insertModelingManagement(ModelingManagementFormDTO modelingManagementFormDTO) {
        modelingManagementFormDTO.setCreateTime(DateUtils.getNowDate());
        modelingManagementFormDTO.setCreateBy(SecurityUtils.getUserId());
        modelingManagementMapper.insertModelingManagement(modelingManagementFormDTO);
        return modelingManagementFormDTO;
    }

    /**
     * 修改建模管理
     *
     * @param modelingManagementFormDTO 建模管理
     * @return 结果
     */
    @Override
    public int updateModelingManagement(ModelingManagementFormDTO modelingManagementFormDTO) {
        modelingManagementFormDTO.setUpdateTime(DateUtils.getNowDate());
        modelingManagementFormDTO.setUpdateBy(SecurityUtils.getUserId());
        return modelingManagementMapper.updateModelingManagement(modelingManagementFormDTO);
    }

    /**
     * 批量删除建模管理
     *
     * @param modelingIds 需要删除的建模管理主键
     * @return 结果
     */
    @Override
    public int deleteModelingManagementByModelingIds(Long[] modelingIds) {
        for (Long modelingId : modelingIds) {
            if (numberManagementMapper.countNumberManagementByModelingId(modelingId) > 0) {
                ModelingManagementVO modelingManagementVO = modelingManagementMapper.selectModelingManagementByModelingId(modelingId);
                throw new ServiceException(String.format("%1$s已使用,不能删除", modelingManagementVO.getFilename()));
            }
        }
        return modelingManagementMapper.deleteModelingManagementByModelingIds(modelingIds);
    }

    /**
     * 删除建模管理信息
     *
     * @param modelingId 建模管理主键
     * @return 结果
     */
    @Override
    public int deleteModelingManagementByModelingId(Long modelingId) {
        return modelingManagementMapper.deleteModelingManagementByModelingId(modelingId);
    }
}
